package data.set.implementation;

import data.set.OfflineSetIterator;


/**
 * Builds an OfflineSetIterator which is a sampling of another set
 * @author guillaumes
 *
 * @param <X>
 */
public class OfflineSetIteratorSampling<X> implements OfflineSetIterator<X>
{
	private OfflineSetIterator<X> iterator;
	private double proba;
	private X current_X;
	private int current_id;
	
	/**
	 * 
	 * @param iterator
	 * @param proba
	 */
	public OfflineSetIteratorSampling(OfflineSetIterator<X> iterator,double proba)
	{
		this.iterator=iterator;
		this.proba=proba;
		go();
	}
	
	private void go()
	{
		while(iterator.hasNext())
		{
			iterator.next();
			if (Math.random()<proba) 
			{	
				return;
			}
		}
		current_X=null;
		current_id=-1;
	}
	
	
	public int getCurrentId() {
		return current_id;
	}

	
	public X getCurrentObject() {
		return current_X;
	}

	
	public boolean hasNext() {
		return (current_X != null);
	}

	
	public void next() {
		current_X=iterator.getCurrentObject();
		current_id=iterator.getCurrentId();
		go();
	}
}
